<?php

namespace app\api\model;

use app\common\model\ProductProgram;
use think\Db;
use think\Model;

class Product extends Model
{
    private static $instance = null;

    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    public static function getInstance()
    {
        if (!isset(self::$instance)) {
            $obj = new static();
            self::$instance = $obj;
            return $obj;
        } else {
            return self::$instance;
        }
    }

    /**
     * 获取产品信息
     */
    public function getProductInfo($where = [])
    {
        $info = Db::name('product')->alias('p')
            ->where($where)
            ->find();
        return $info;
    }

    /**
     * 获取工种列表方案
     */
    public function getProductWork($where = [])
    {
        $info = Db::name('type_work')->alias('tw')
            ->where($where)
            ->find();
        return $info;
    }

    /**
     * 获取对应产品可选择的月份
     * @param type $proId
     * @return type
     */
    public function getCanChooseMonth($proId)
    {
        $canChooseMonth = \app\common\model\ProductOther::where('pro_id', $proId)->value('can_choose_month');

        $month = \app\common\model\Product::$month;
        if (!empty($canChooseMonth)) {
            $array = json_decode($canChooseMonth, true);
            $return = [];
            foreach ($array as $m) {
                if (!empty($month[$m])) {
                    $return[$m] = $month[$m];
                }
            }
            return $return;
        }
        return $month;
    }

    /**
     * 获取产品方案
     * @param type $id
     * @return type
     */
    public function getProgram($proId)
    {
        $productProgram = Db::name('program')->alias('pr')
            ->join('t_product_program pp', 'pr.id = pp.program_id', 'left')
            ->where(['pr.pro_id' => $proId, 'pr.status' => ProductProgram::STATUS_OK])
            ->field('pr.*, pp.career_work, pp.year_money, pp.safeguard, pp.id AS gramId')
            ->order('pp.year_money')
//            ->group('pp.safeguard')
            ->select();

        return $productProgram;
    }

}